package com.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.commons.dao.StringUtils;
import com.commons.po.Customer;
import com.commons.po.DeptPo;
import com.commons.po.StatusMsg;
import com.commons.web.servlet.BaseServlet;
import com.csm.dao.CustomerDao;
import com.csm.dao.impl.CustomerDaoImpl;
import com.csm.services.CustomerService;
import com.csm.services.impl.CustomerServiceImpl;
import com.hjf.service.impl.DeptPoServiceImpl;

public class CustomerServlet extends BaseServlet{
	CustomerService cs = new CustomerServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1、获取操作符
		String opr=req.getParameter("opr");
		
		if("add".equals(opr)){
			List<DeptPo> deptList = new DeptPoServiceImpl().findByCondition();
			req.setAttribute("deptList", deptList);
			req.getRequestDispatcher("jsp/sys/addCustomer/addCustomer.jsp").forward(req, resp);
		}else if("save".equals(opr)){
			save(req,resp);
		}else if("loadData".equals(opr)){
			loadData(req,resp);
			List<DeptPo> deptList = new DeptPoServiceImpl().findByCondition();
			req.setAttribute("deptList", deptList);
			req.getRequestDispatcher("jsp/sys/updateCustomer/updateCustomer.jsp").forward(req, resp);
		}else if("update".equals(opr)){
			
			update(req,resp);
		}else{
			/*
			 * 1.查询statusmsg所有数据
			 * 2.查询customer所有数据
			 * 3.返回数据
			 */
			//1.查询sttusmsg所有数据
			//2.查询customer所有数据
			List<StatusMsg> statulist=cs.findAll();
			req.setAttribute("statulist", statulist);
			req.getRequestDispatcher("jsp/sys/CustomerManage/CustomerManage.jsp").forward(req, resp);
			
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param resp
	 * @throws IOException
	 */
	private void loadData(HttpServletRequest request, HttpServletResponse resp) throws IOException{
		Customer update = cs.findCustomerById(StringUtils.str2Integer(request.getParameter("CustomerId"), false, null));
		request.setAttribute("update", update);
	}
	private void update(HttpServletRequest request, HttpServletResponse resp) throws IOException {
		System.out.println("update...");
		int rows = 0;
		String msg = "";// 错误消息的变量
			try {
				java.sql.Date currDate=new java.sql.Date(System.currentTimeMillis());//当前系统时间
				//Customer currCustomer=this.getCurrUser(request.getSession());//获得当前登录的用户
				StatusMsg currStatus = this.getCurrUser(request.getSession());//获得当前登录的用户
				// 2、获得数据
				Customer user = new Customer();
				user.setId(Integer.parseInt(request.getParameter("customer_id")));
				user.setCustomer_name(request.getParameter("customer_name"));
				user.setCustomer_telephone(request.getParameter("customer_telephone"));
				user.setCustomer_email(request.getParameter("customer_email"));
				System.out.println(Integer.parseInt(request.getParameter("customer_dept")));
				user.setCustomer_dept(Integer.parseInt(request.getParameter("customer_dept")));
				user.setCustomer_introduce(request.getParameter("customer_introduce"));
				
//				currStatus.setId(currStatus.getId());
				currStatus.setCreated_date(currDate);
//				msg1.setUpdated_id(currCustomer.get);
				currStatus.setUpdated_date(currDate);
				currStatus.setCustomer_password("1234");
				currStatus.setIs_active(1);
				if(StringUtils.strIsNull(user.getCustomer_name())){
					throw new RuntimeException("用户姓名不能为空");
				}
				//4、保存数据：调用底层dao实现
				CustomerService cs = new CustomerServiceImpl();
				rows = cs.updateCustomer(user);
			}catch(Exception e){
				e.printStackTrace();
				msg="修改失败："+e.getMessage();
			}
			
			resp.setCharacterEncoding("utf-8");
			resp.setContentType("text/html;charset=utf-8");
			
			PrintWriter out=resp.getWriter();
			if(rows>0){
				out.println("<script type='text/javascript'>alert('修改成功');location.href='CustomerServlet';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('"+msg+"');history.back();</script>");
			}
		}
	
	private void save(HttpServletRequest request, HttpServletResponse resp) throws IOException {
		System.out.println("save...");
		int rows = 0;
		String msg = "";// 错误消息的变量
			try {
				java.sql.Date currDate=new java.sql.Date(System.currentTimeMillis());//当前系统时间
				//Customer currCustomer=this.getCurrUser(request.getSession());//获得当前登录的用户
				StatusMsg currStatus = this.getCurrUser(request.getSession());//获得当前登录的用户
				// 2、获得数据
				Customer user = new Customer();
//				java.sql.Date currDate=new java.sql.Date(System.currentTimeMillis());//当前系统时间
//				Customer currCustomer=this.getCurrUser(request.getSession());//获得当前登录的用户
				// 2、获得数据
//				Customer user = new Customer();
				//user.setCustomer_id(Integer.parseInt(request.getParameter("customer_id")));
				user.setCustomer_name(request.getParameter("customer_name"));
				user.setCustomer_telephone(request.getParameter("customer_telephone"));
				user.setCustomer_email(request.getParameter("customer_email"));
				System.out.println(Integer.parseInt(request.getParameter("customer_dept")));
				user.setCustomer_dept(Integer.parseInt(request.getParameter("customer_dept")));
				user.setCustomer_introduce(request.getParameter("customer_introduce"));
				
//				user.setCreated_id(currCustomer.getCustomer_id());
//				user.setCreated_date(currDate);
//				user.setUpdated_id(currCustomer.getCustomer_id());
//				user.setUpdated_date(currDate);
//				user.setCustomer_password("1234");
//				user.setIs_active(1);
//				currStatus.setId(currStatus.getId());
				currStatus.setCreated_date(currDate);
//				msg1.setUpdated_id(currCustomer.get);
				currStatus.setUpdated_date(currDate);
				currStatus.setCustomer_password("1234");
				currStatus.setIs_active(1);
				boolean isFlag = cs.saveStatusMsg(currStatus);
				System.out.println(isFlag);
				if(StringUtils.strIsNull(user.getCustomer_name())){
					throw new RuntimeException("用户姓名不能为空");
				}
				//4、保存数据：调用底层dao实现
				CustomerService cs = new CustomerServiceImpl();
				rows = cs.addCustomer(user);
			}catch(Exception e){
				e.printStackTrace();
				msg="保存失败："+e.getMessage();
			}
			
			resp.setCharacterEncoding("utf-8");
			resp.setContentType("text/html;charset=utf-8");
			
			PrintWriter out=resp.getWriter();
			if(rows>0){
				out.println("<script type='text/javascript'>alert('保存成功');location.href='CustomerServlet';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('"+msg+"');history.back();</script>");
			}
		}
		
		
}
